package gameObjects;

import com.jme.image.Texture;
import com.jme.image.Texture.MagnificationFilter;
import com.jme.image.Texture.MinificationFilter;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.Text;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.BlendState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;

/**
 * Classe che gestisce l'HUD sullo schermo di gioco
 * @author Massimiliano Palermo
 *
 */
public class Hud extends Node{
	private static final long serialVersionUID = 18042010191046L;
	private Quad			panel;
	private int 			x1,y1,x2,y2;
	private DisplaySystem 	display = DisplaySystem.getDisplaySystem();
	private Renderer 		renderer = display.getRenderer();
	private Text   			coin;
	private StringBuffer    strBuf;
	
	public Hud()
	{
		//super("HUD");
		System.out.println("HUD");
		//Carico la texture
		final TextureState ts = display.getRenderer().createTextureState();
		final Texture      t  = TextureManager.loadTexture(
									this.getClass().getClassLoader().getResource("textures/hud 640x480.png"),
									MinificationFilter.Trilinear,
									MagnificationFilter.Bilinear,
									1.0f, true);
		System.out.println("TEXTURE->"+t.getImage().getWidth()+"x"+t.getImage().getHeight());
		//Creo l'oggetto
		x1=display.getWidth()/2; y1=display.getHeight()-16; x2=display.getWidth(); y2=32;
		panel = new Quad("PANEL",(float)x2,(float)y2);
		panel.setRenderQueueMode(Renderer.QUEUE_ORTHO);
		panel.setLightCombineMode(Spatial.LightCombineMode.Off);
		panel.setLocalTranslation(x1,y1,0.0f);
		panel.updateRenderState();
		
		//Imposto il materiale per il rendering dell'oggetto.
		MaterialState ms = renderer.createMaterialState();
        ms.setAmbient(new ColorRGBA(1,1,1,0));
        ms.setDiffuse(new ColorRGBA(0,0,0,1));
        ms.setEnabled(true);
        panel.setRenderState(ms);
		
		//Applico la texture
		ts.setTexture(t);
		ts.getTexture().getImage().getWidth(); 
		ts.getTexture().getImage().getHeight();
		ts.setEnabled(true);
		setUpTransparency();
		panel.setRenderState(ts);
		this.attachChild(panel);
	}
	
	/**
	 * Imposta la trasparenza dell'HUD
	 * NB: la texture deve avere una zona trasparente!!!
	 */
	private void setUpTransparency()
	{
		BlendState blendState = display.getRenderer().createBlendState();
		blendState.setBlendEnabled( true );
		blendState.setSourceFunction( BlendState.SourceFunction.SourceAlpha );
		blendState.setDestinationFunction( BlendState.DestinationFunction.OneMinusSourceAlpha );
		blendState.setTestEnabled( true );
		blendState.setTestFunction( BlendState.TestFunction.GreaterThanOrEqualTo );
		blendState.setEnabled( true );
		
		panel.setRenderState(blendState);
	}
	
	public void setCoinText(String text)
	{
		strBuf.setLength(0);
		strBuf.append(text);
		coin.print(strBuf);
		coin.updateRenderState();
		
	}
	
}